﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TaskManage
{
    public partial class FormTaskAdd : Form
    {
        public string uid;
        public FormTaskAdd()
        {
            InitializeComponent();
        }

        private void FormTaskAdd_Load(object sender, EventArgs e)
        {
            dateTimePicker1.Format = DateTimePickerFormat.Custom;
            dateTimePicker1.CustomFormat = "yyyy-MM-dd HH:mm:ss";
            dateTimePicker2.Format = DateTimePickerFormat.Custom;
            dateTimePicker2.CustomFormat = "yyyy-MM-dd HH:mm:ss";
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string tTitle = this.textBoxTitle.Text.ToString();
            string tReceiver = this.textBoxTaskreceiver.Text.ToString();
            string tContent = this.richTextBoxContent.Text.ToString();
            string tstaTime = this.dateTimePicker1.Text.ToString();
            string tendTime = this.dateTimePicker2.Text.ToString();

            if (tTitle == "" || tReceiver == "" || tContent == "")
            {
                MessageBox.Show("任务主题、任务接受者、任务具体内容不能为空！", "提示");
                return;
            }
            if (tendTime.CompareTo(tstaTime) < 0)
            {
                MessageBox.Show("任务开始时间应该晚于或等于任务结束时间","提示");
                return;
            }

            DBConnect dbConn = new DBConnect();
            string qstr;
            qstr = "SELECT uid FROM users_info WHERE uname = '" + tReceiver + "'";
            DataSet ds = dbConn.Query(qstr);
            DataTable dtb = ds.Tables[0];
            DataRow drow = dtb.Rows[0];
            if (dtb.Rows.Count == 0)
            {
                MessageBox.Show("任务接受者不存在！", "提示");
                return;
            }

            DateTime dt = DateTime.Now;
            qstr = "INSERT INTO tasks_info (task_name,task_content,task_pubTime,task_staTime,task_endTime,task_pubId,task_revId) " +
                "VALUES ('" + tTitle + "','" + tContent + "','" + dt.ToString() + "','" + tstaTime + "','" + tendTime + 
                "','" + this.uid + "','" + drow[0].ToString() + "')";
            dbConn.Insert(qstr);
            MessageBox.Show("任务添加成功", "提示");
        }
    }
}
